/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.enac.geoapp.wims;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import edu.enac.geoapp.wims.model.ProductType;
import edu.enac.geoapp.wims.model.WeatherProduct;

/**
 * Content handler for the parsing of the common fields of the various GML files
 * @author mathieu
 *
 */
public class GlobalDataContentHandler implements ContentHandler {
	private WeatherProduct product;
	private boolean inForecastSet = false;
	private boolean inGeometry = false;
	
	private StringBuffer buffer;
	private String currentName;
	
	/**
	 * Constructor specifying the product type
	 * @param product
	 */
	public GlobalDataContentHandler(WeatherProduct product) {
		this.product = product;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String content = new String(ch, start, length);
		if (!inGeometry)
			content = content.trim();
		if (buffer != null)
			buffer.append(content);
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (!inForecastSet) {
			if (localName.contains("productType")) {
				String type = buffer.toString().toLowerCase();
				if(type.contains("cat"))
					product.setProductType(ProductType.CAT);
				else if (type.contains("ice"))
					product.setProductType(ProductType.ICE);
				else
					product.setProductType(ProductType.CB);
				buffer = null;
			}
			if (localName.contains("originatingCentre")) {
				product.setOriginatingCentre(buffer.toString());
				buffer = null;
			}
			if (localName.contains("productScale")) {
				product.setScale(buffer.toString());
				buffer = null;
			}
			if (localName.contains("issuedTime")) {
				product.setIssuedTime(buffer.toString());
				buffer = null;
			}
		}
	}

	

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		this.currentName = localName;
		// test where I am in the file
		if (localName.contains("forecastSet"))
			inForecastSet = true;
		
		// depending on where I am, re-init the buffer
		if (!inForecastSet) {
			initBuffFor("productType");
			initBuffFor("originatingCentre");
			initBuffFor("productScale");
			initBuffFor("issuedTime");
		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	public WeatherProduct getProduct() {
		return product;
	}

	private void initBuffFor(String strToTest) {
		if (currentName.contains(strToTest))
			buffer = new StringBuffer();
	}
}
